package com.sduept.nwld.dataserver.controller.document;

import com.sduept.bigdata.document.entity.core.CommonDocument;
import com.sduept.bigdata.document.entity.core.CommonDocumentDir;
import com.sduept.bigdata.document.services.CommonDocumentService;
import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.nwld.dataserver.util.LibTreeNode;
import org.primefaces.PrimeFaces;
import org.primefaces.event.NodeExpandEvent;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Named
@ViewScoped
public class CommonDocumentSelectorController extends AbstractController implements	Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1720928987182910018L;
	@Autowired
	private CommonDocumentService docService;

	private TreeNode root;
	private TreeNode selectedNode;
	//构建那种类型的树
	private String treeType;
	
	private List<CommonDocumentDir> documentDirs;

	public static final String TREE_TYPE_DIR = "CommonDocumentDir";
	public static final String TREE_TYPE_DOC = "Document";


	@PostConstruct
	public void init() {
		try {
			String rootType = getParameter("rootTypes");
			if("evaluate".equals(rootType))
				documentDirs=docService.findAllDocumentDirByType("evaluate");
			else
				documentDirs = docService.findAllDocumentDir();
			root = new DefaultTreeNode();
			List<CommonDocumentDir> rootDirs = new ArrayList<>();
			for (CommonDocumentDir dir : documentDirs) {
				if (dir.getPid() == null) {
					rootDirs.add(dir);
				}
			}
			sortDirByOrderNo(rootDirs);
			for (CommonDocumentDir d : rootDirs) {
				new LibTreeNode(CommonDocumentDir.class.getSimpleName(), d, root);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void onNodeExpand(NodeExpandEvent event){
		TreeNode node = event.getTreeNode();
		expand(node);
	}
	
	private void expand(TreeNode node){
		try {
			if (TREE_TYPE_DIR.equals(node.getType())) {
				CommonDocumentDir currentObj = (CommonDocumentDir) node.getData();
				List<CommonDocumentDir> dirtemps = new ArrayList<CommonDocumentDir>();
				for (CommonDocumentDir d : documentDirs) {
					if (d.getPid() != null && d.getPid().equals(currentObj.getId())) {
						dirtemps.add(d);
					}
				}
				List<CommonDocument> doctemps=new ArrayList<CommonDocument>();
				doctemps=docService.findDocumensByDirId(currentObj.getId());
				sortDirByOrderNo(dirtemps);
				sortDocByOrderNo(doctemps);
				for (CommonDocumentDir dir : dirtemps) {
					new LibTreeNode("CommonDocumentDir", dir, node);
				}
				for (CommonDocument doc: doctemps) {
					new LibTreeNode("Document", doc, node);
				}
			}
		}catch(Exception e) {
			e.printStackTrace();
		}
	}

	public void selectDir() {
		//PrimeFaces.current().dialog().closeDynamic(selectedNode.getData());
		PrimeFaces.current().dialog().closeDynamic(selectedNode.getData());
	}
	
	/*
	 * 按orderNo升序 排序
	 */
	private void sortDirByOrderNo(List<CommonDocumentDir> dirs) {
		Collections.sort(dirs, new Comparator<CommonDocumentDir>() {
			@Override
			public int compare(CommonDocumentDir o1, CommonDocumentDir o2) {
				return o1.getOrderNo() - o2.getOrderNo();
			}
		});
	}
	/*
	 * 按orderNo升序 排序
	 */
	private void sortDocByOrderNo(List<CommonDocument> docs) {
		Collections.sort(docs, new Comparator<CommonDocument>() {
			@Override
			public int compare(CommonDocument o1, CommonDocument o2) {
				return o1.getOrderNo() - o2.getOrderNo();
			}
		});
	}

	public TreeNode getRoot() {
		return root;
	}

	public void setRoot(TreeNode root) {
		this.root = root;
	}

	public TreeNode getSelectedNode() {
		return selectedNode;
	}

	public void setSelectedNode(TreeNode selectedNode) {
		this.selectedNode = selectedNode;
	}
	
	
	public List<CommonDocumentDir> getDocumentDirs() {
		return documentDirs;
	}
	
	public void setDocumentDirs(List<CommonDocumentDir> documentDirs) {
		this.documentDirs = documentDirs;
	}

	public String getTreeType() {
		return treeType;
	}

	public void setTreeType(String treeType) {
		this.treeType = treeType;
	}
	

}
